package com.pwt.service;

import com.pwt.entity.FilterUnit;
import com.pwt.model.ShiftModel;
import com.pwt.repository.FilterUnitRepository;
import com.pwt.security.utils.JWTUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * FilterUnit service层
 *
 * @author auto generated
 * @date 2021-10-11 17:36:59
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FilterUnitService {

    private FilterUnitRepository filterUnitRepository;

    public FilterUnitService(FilterUnitRepository filterUnitRepository) {
        this.filterUnitRepository = filterUnitRepository;
    }

    public FilterUnit update(FilterUnit filterUnit){
        String username = JWTUtils.getToken().getUsername();
        FilterUnit filter = this.filterUnitRepository.getFilterUnitByCreateBy(username);
        if(filter == null){
            filter = new FilterUnit();
        }
        filter.setName(filterUnit.getName());
        filter.setImportance(filterUnit.getImportance());
        return this.filterUnitRepository.save(filter);
    }


    public FilterUnit getFilterUnit(){
        String username = JWTUtils.getToken().getUsername();
        return this.filterUnitRepository.getFilterUnitByCreateBy(username);
    }


    public List<ShiftModel> getShiftList(String startTime,String endTime) {
        List<ShiftModel> list = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = null;
        try {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date start = simpleDateFormat.parse(startTime);
            Date end  = simpleDateFormat.parse(endTime);
            SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Long day = (end.getTime()-start.getTime())/(24*60*60*1000)+1;

            for(int i = 0;i <= day; i++){
                if(i == day){
                    ShiftModel shiftModel = new ShiftModel();
                    Long nextTime = end.getTime();
                    String next = simpleDateFormat.format(new Date(Long.parseLong(String.valueOf(nextTime))));
                    shiftModel.setStartTime(simple.parse(next+" 08:00:00"));
                    shiftModel.setEndTime(simple.parse(next+" 20:00:00"));
                    list.add(shiftModel);
                }else{
                    ShiftModel shiftModel1 = new ShiftModel();
                    Long nextTime =  start.getTime() + Long.valueOf(i)*24*60*60*1000;
                    String next = simpleDateFormat.format(new Date(nextTime));
                    shiftModel1.setStartTime(simple.parse(next+" 08:00:00"));
                    shiftModel1.setEndTime(simple.parse(next+" 20:00:00"));
                    list.add(shiftModel1);

                    Long twoTime = nextTime+24*60*60*1000;
                    String twoDay = simpleDateFormat.format(new Date(twoTime));
                    ShiftModel shiftModel3 = new ShiftModel();
                    shiftModel3.setStartTime(simple.parse(next+" 20:00:00"));
                    shiftModel3.setEndTime(simple.parse(twoDay+" 08:00:00"));
                    list.add(shiftModel3);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

}
